package view.admin;

import controllers.RoomController;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import entity.Room;
import constants.*;
import java.util.ArrayList;
import controllers.*;
import javax.swing.*;
public class RoomOperate extends JDialog{
	
	private JTextField     tfRoomName 	= new JTextField(10);
	private JComboBox<String>	cbType ;
	String[] typeNames;
	
	private JButton	btAddRoom	= new JButton("Xác nhận");
	private JButton btCancel	= new JButton("Hủy bỏ");
	private ArrayList<entity.RoomType> roomTypes = RoomController.getAllType();
	entity.Room room;
	
	private boolean isOk = false;
	private int currentRoom;
	public RoomOperate(JDialog parent, String title, Room room,int currentRoom){
		super(parent, title, true);
                this.currentRoom=currentRoom;
		if (currentRoom==-1)
                    this.room = new Room();
		else 
                    this.room = room;
		
		setSize(450, 160);
		setLocationRelativeTo(parent);
		setResizable(false);
		getTypeList();		
		
		initUI();
	}
	
	private void getTypeList(){
		typeNames = new String[roomTypes.size()];
		for (int i=0; i<roomTypes.size(); i++){
			typeNames[i] = roomTypes.get(i).getName();
		}
		cbType = new JComboBox<String>(typeNames);
	}
	
	public void initUI(){
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 20, 10, 20);
		gbc.fill = GridBagConstraints.HORIZONTAL;
		
		
		gbc.gridy = 0; gbc.gridx = 0;  add(new JLabel("Tên phòng"), gbc);
		gbc.gridy = 1; gbc.gridx = 0;	add(new JLabel("Loại phòng"), gbc);
		
		gbc.weightx = 1.0;
		gbc.gridy = 0; gbc.gridx = 1;	add(tfRoomName, gbc);
		gbc.gridy = 1; gbc.gridx = 1;	add(cbType, gbc);
		gbc.weightx = 0;
		
		gbc.gridy = 0; gbc.gridx = 2; 	add(btAddRoom, gbc);
		gbc.gridy = 1;	gbc.gridx = 2; add(btCancel, gbc);
			
		btAddRoom.addActionListener(new ButtonHandler());
		btCancel.addActionListener(new ButtonHandler());
		
		tfRoomName.setText(room.getName());
	}
	
	public boolean getResult(){
		setVisible(true);
		return isOk;
	}
        
	public Room getRoom(){
                return room;
        }
	class ButtonHandler implements ActionListener{
		public void actionPerformed(ActionEvent event){
			if (event.getSource() == btAddRoom){
				if (!Validate.isEmpty(tfRoomName.getText()) ||
					(cbType.getSelectedIndex() < 0)||!Validate.isRoomNameExit(room.getId(),tfRoomName.getText())) {
				 JOptionPane.showMessageDialog(null,Validate.message);
                                }else{
				room.setName(tfRoomName.getText());
				room.setType(cbType.getSelectedIndex()+1);                                
				room.setStatus(Constants.EMPTY);
                                System.out.println(currentRoom);
                                if(currentRoom==-1) 
                                    RoomController.insert(room);
                                else
                                    RoomController.update(room.getId(), room);
				
                                isOk = true;
				dispose();
                                }
			}else if (event.getSource() == btCancel){
				dispose();
			}
		}
	}
}
